﻿using Pay.DTO;
using Pay.IService;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Security;
using Pay.TO;
using System;

namespace Pay.Distribution.Controllers
{
    [RoutePrefix("account")]
    public class AccountController : GeneralController {
        private IManageService _iManageService;
        private IRoleService _iRoleService;

        public AccountController(IManageService iManageService, IRoleService iRoleService) {
            _iManageService = iManageService;
            _iRoleService = iRoleService;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="login_model"></param>
        /// <param name="code"></param>
        /// <param name="remember"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        [Route("login")]
        public JsonResult Login(login_model login_model, string code, bool remember = false) {
            if (string.IsNullOrEmpty(code))
                return Json(ResultToJson.Json<object>("验证码没有填写"), JsonRequestBehavior.AllowGet);
            if (Session["Code"] == null)
                return Json(ResultToJson.Json<object>("你确认在登录吗"), JsonRequestBehavior.AllowGet);
            if (!string.IsNullOrEmpty(code) && code.ToLower() != Session["Code"].ToString().ToLower())
                return Json(ResultToJson.Json<object>("验证码填写的不正确"), JsonRequestBehavior.AllowGet);

            var manage = _iManageService.Login(login_model.mobile, login_model.password);
            List<string> roles = new List<string> { "Admin" };
            //创建用户COOKIE
            FormsCookieSave(manage.mobile, new userCookie { user_id =Convert.ToInt64(manage.merchant_id), roles = roles, role_id = manage.role_id }, remember);
            return Json(ResultToJson.Json<object>(new {
                nick = manage.nick,
                mobile = manage.mobile,
                role_name = manage.role_id.HasValue ? _iRoleService.Get(manage.role_id.Value).name : "超级管理员",
                is_admin = true
            }), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 资料
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("profile")]
        public JsonResult ProfileInfo() {
            var manage = _iManageService.Get(currentUser.user_id);
            return Json(ResultToJson.Json(manage), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 保存资料
        /// </summary>
        /// <param name="nick"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("save_profile")]
        public JsonResult ProfileSave(string nick) {
            var manage = _iManageService.Get(currentUser.user_id);
            manage.nick = nick;
            int result = _iManageService.Update(manage);

            if (result >= 0)
                return Json(ResultToJson.Json("保存成功", result), JsonRequestBehavior.AllowGet);
            return Json(ResultToJson.Json<int>("保存失败"), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 退出
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [Route("logout")]
        public RedirectResult Logout(string return_url) {
            FormsAuthentication.SignOut();
            if (!string.IsNullOrEmpty(return_url))
                return Redirect(return_url);
            return Redirect("/");
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="password"></param>
        /// <param name="re_password"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("change_pwd")]
        public JsonResult ChangePwd(string old_password, string password, string re_password) {
            var result = _iManageService.ChangePwd(currentUser.user_id, old_password, password, re_password);
            if (result >= 0)
                return Json(ResultToJson.Json("修改成功", result), JsonRequestBehavior.AllowGet);
            return Json(ResultToJson.Json<bool>("修改失败"), JsonRequestBehavior.AllowGet);
        }
    }
}